/* 读取behavior3生成的json文件 */
var BTNTreeReader = function(){
    var self = this;

    self._rootid = "";
    self._rootnode = undefined;

    self._infolist = undefined; //编辑器节点信息列表

    /* Node-Types */
    /* 
    Composites[Seq, Sel]
    Action[Act]
    Condition[Conf]
    */
    self._nodeFactory = function(nodeinfo, funit){
        //chk name
        var id = nodeinfo["id"]
        var name = nodeinfo["name"];
        var desc = nodeinfo["description"]
        var isleaf = true;
        var children;
        var node;
        // cc.log("node parse, --->>id is %s, desc is %s", id, desc);
        switch(name){
            case "Sel":
                isleaf = false;
                node = new BTNSeleNode(id);
                break;
            case "Seq":
                isleaf = false;
                node = new BTNSeqNode(id);
                break;
            case "Conf":
                // cc.log("in conf dead", funit);
                node = new UNIT_AI.conf[desc](id, funit)
                break;
            case "Act":
                // node = new BTNActionNode(id);
                node = new UNIT_AI.act[desc](id, funit)
                break;
            default:
                break;
        }
        //chk child
        if (!isleaf){
            children = nodeinfo["children"]
            for (var childid in children){
                node.addChild(self._nodeFactory(self._infolist[children[childid]], funit))
            }
        }
        return node;
    }

    self.parse = function(jsdic, funit){
        var infolist = jsdic["nodes"]
        self._infolist = infolist
        self._rootid   = jsdic["root"]
        
        self._rootnode = self._nodeFactory(infolist[self._rootid], funit)
        // cc.log("==========>>>> in btntree", self._rootnode);
        return self._rootnode
    }
}   